#include <sys/time.h>
#include <stdio.h>
#include "lvgl.h"
#include "lv_conf.h"
#include "sdl/sdl.h"
//#include "ui.h"

#define DISP_BUF_SIZE 800*480*2

uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if(start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }
    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}

//初始化函数
void init_func()
{
    //sdl仿真平台初始化
    sdl_init();

    //显示缓冲区初始化
    static lv_color_t buf[DISP_BUF_SIZE]; //显示缓冲区
    static lv_color_t buf1[DISP_BUF_SIZE]; //显示缓冲区
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, buf1, DISP_BUF_SIZE);

    //显示驱动初始化
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf   = &disp_buf;
    disp_drv.flush_cb = sdl_display_flush;
    disp_drv.hor_res    = 800;
    disp_drv.ver_res    = 480;
    lv_disp_drv_register(&disp_drv);

    //输入设备驱动初始化
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1); 
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;  //设置输入事件类型
    indev_drv_1.read_cb = sdl_mouse_read;
    lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1);
}

void ui_test()
{
    //通过父窗体（屏幕）创建最底层窗体mainobj（注意是指针类型）
    lv_obj_t *mainobj =lv_obj_create(lv_scr_act());//屏幕大小为800*480
    //设置mainobj尺寸
    lv_obj_set_size(mainobj, 400, 240);
    //设置坐标
    lv_obj_set_pos(mainobj, 200, 120);
    
    //在父窗体mainobj上新建一个按钮（注意是指针类型）
    lv_obj_t *button = lv_btn_create(mainobj);
    //设置按钮大小和位置
    lv_obj_set_size(button, 100, 50);
    lv_obj_set_pos(button, 50, 40);

    //在父窗体按钮上添加文本（注意是指针类型）
    lv_obj_t *text = lv_label_create(button);
    lv_label_set_text(text, "cancel");
    //设置文本大小和位置
    lv_obj_set_size(text, 90, 40);
    lv_obj_set_pos(text, 5, 5);

    lv_obj_t *Arc_bar = lv_arc_create(mainobj);
    lv_obj_set_size(Arc_bar, 200, 50);
    lv_obj_set_pos(Arc_bar, 200, 100);
}

void UI_style()
{
    
}

int main(void)
{
    lv_init();
    //初始化
    init_func();
    //测试
    //ui_test();

    while(1) 
    {
        lv_timer_handler();
        usleep(5000);
    }

    return 0;
}

